

// data() { return { CountDownConfig: { label: '', time: 15, isRun: false } }}

/**
 *
 * @param {*} option  { defaultLabel: '发送验证码', reoperateLabel: '',  time: 15, startEvent: () => {}, endEvent: ()=>{}}
 * @returns
 */
const useCountDown = (pageSelf, option) => {
  pageSelf.CountDownConfig.label = option.defaultLabel || '发送验证码'
  pageSelf.CountDownConfig.time = option.time
  pageSelf.CountDownConfig.isRun = false

  // const label_ = pageSelf.CountDownConfig.label // ref(option.defaultLabel || '发送验证码')
  // const seconds =pageSelf.CountDownConfig.time
  // const isRun = pageSelf.CountDownConfig.isRun
  const startEvent = ()=>{
    pageSelf.CountDownConfig.isRun = true
      pageSelf.CountDownConfig.time = option.time
      option.startEvent && option.startEvent()
      if(pageSelf.isType.isNullOrUnDef(option.defaultLabel) || pageSelf.isType.isEmpty(option.defaultLabel )){
         pageSelf.CountDownConfig.label = `${pageSelf.CountDownConfig.time} 秒后重试`
      }else{
         pageSelf.CountDownConfig.label = `${pageSelf.CountDownConfig.time} 秒${option.reoperateLabel}`
      }
      const temp = setInterval(()=>{
          pageSelf.CountDownConfig.time = pageSelf.CountDownConfig.time - 1
          if(pageSelf.isType.isNullOrUnDef(option.defaultLabel) || pageSelf.isType.isEmpty(option.defaultLabel )){
             pageSelf.CountDownConfig.label = `${pageSelf.CountDownConfig.time} 秒后重试`
          }else{
             pageSelf.CountDownConfig.label = `${pageSelf.CountDownConfig.time} 秒${option.reoperateLabel}`
          }
          if(pageSelf.CountDownConfig.time<0){
              clearInterval(temp)
              if(pageSelf.isType.isNullOrUnDef(option.defaultLabel) || pageSelf.isType.isEmpty(option.defaultLabel )){
                 pageSelf.CountDownConfig.label = option.reoperateLabel || '重新发送验证码'
              }else{
                 pageSelf.CountDownConfig.label = option.defaultLabel || ''
              }
              option.endEvent && option.endEvent()
              pageSelf.CountDownConfig.isRun = false
          }
      }, 1000)
  }

  return {label:pageSelf.CountDownConfig.label,startTimingEvent:()=>{!pageSelf.CountDownConfig.isRun && startEvent()}}
}

export default useCountDown
